package com.polaris.live.ui.user_space.proxy

import android.view.TextureView
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.util.toInt
import com.polaris.live.im.sfs.bean.pusher.UserStateMessage
import com.polaris.live.manager.UserStateManager
import com.polaris.live.resp.back_resp.InfoOtherResp
import com.polaris.live.utils.state.isStartedLive
import com.polaris.live.utils.state.isStoppedLive
import com.polaris.live.utils.state.modifyState
import com.polaris.live.utils.zego.LiveActivityIdentifier
import com.polaris.live.utils.zego.LivePlayerLifecycle
import com.polaris.live.utils.zego.listener.OnLivePlayListener
import com.polaris.live.utils.zego.listener.OnLivePlayViewAlphaImpl
import com.polaris.live.viewmodel.listeners.OnStreamLiveDataListener
import com.polaris.live.viewmodel.listeners.getUserLiveStream

/**
 * UserStateProxy
 *
 * @author Created by lucas on 2023/11/28 14:01
 */
class UserStatePlayerProxy<VM>(
    private val mActivity: AppCompatActivity,
    private val mViewModel: VM,
    private val mOtherId: Long,
    private val face: LiveActivityIdentifier,

    ) where VM : OnStreamLiveDataListener, VM : BaseViewModel {

    private var mOtherInfo: InfoOtherResp? = null

    private var mLivePlayer: LivePlayerLifecycle? = null

    private var mOnUserStateListener: OnUserStateListener? = null

    /**
     * 初始化
     */
    fun init(otherInfo: InfoOtherResp, viewLayout: View, view: TextureView) {
        viewLayout.visibility = View.GONE

        init(otherInfo, view, OnLivePlayViewAlphaImpl(viewLayout))
    }

    /**
     * 初始化
     */
    fun init(otherInfo: InfoOtherResp, view: TextureView, l: OnLivePlayListener) {
        mOtherInfo = otherInfo

        if (mLivePlayer == null) {
            mLivePlayer = LivePlayerLifecycle.create(
                face,
                mActivity,
                view,
                listener = l
            )
            //如果封面照没有审核通过则不展示直播流
            if (mOtherId != UserManager.getUserId() && isHavaCover()) {
                otherInfo.live?.liveStreamId?.also {
                    mLivePlayer?.startStream(mActivity, it)
                }
            }
        }
    }

    /**
     * 开始订阅
     */
    fun observe(l: OnUserStateListener) {
        if (mOnUserStateListener != null) {
            return
        }

        mOnUserStateListener = l

        mViewModel.userLiveLiveData.observe(mActivity) { bean ->
            bean.playStreamId?.also {
                if (isHavaCover()) {
                    mLivePlayer?.startStream(mActivity, it)
                }
            }

            mOtherInfo?.live?.liveStreamId = bean.playStreamId
        }
        UserStateManager.observe(mActivity, { listOf(mOtherId) }, { messages ->
            if (mOtherInfo == null) {
                return@observe
            }

            messages.firstOrNull()?.also {
                val oldLiving = mOtherInfo?.live?.living?.toInt() ?: 0
                val oldCalling = mOtherInfo?.call?.calling?.toInt() ?: 0
                if (it.state.isStartedLive(oldLiving, oldCalling)) {
                    mViewModel.getUserLiveStream(mOtherId)
                } else if (it.state.isStoppedLive(oldLiving, oldCalling)) {
                    mLivePlayer?.stopStream()
                }

                mOnUserStateListener?.onUserStateChange(it)

                mOtherInfo?.modifyState(it)
            }
        })
    }

    private fun isHavaCover() = mOtherInfo?.coverPhoto?.status == AppConst.CoverPhoto.APPROVED
}

fun interface OnUserStateListener {

    fun onUserStateChange(state: UserStateMessage)

}